<?
	include('common.php');
?>
<?php
    class db
    {
        public $conn=NULL;
        public $result=NULL;
        public $host="localhost";
        public $user="root";
        public $pass="";
        public $database="thammyanan";
        private $author="K H A";
        protected $version="1.1";
        function __construct()
        {
        	if ($this->conn==NULL) {
	            $this->conn=mysql_connect($this->host,$this->user,$this->pass);
	            mysql_select_db($this->database,$this->conn);
	            mysql_query("set names 'utf8'");
        	}
        }
        
   		protected function show_field_db($table) {
    		$query = " SHOW FIELDS FROM ".$table;
			$sql = mysql_query($query) or die(mysql_error());
            $array = array();
			while ($row = mysql_fetch_array($sql)) {
				$array[] = $row;
			}
			return $array;
        }
   		protected function get_data_db($query) {
   		 	$sql = mysql_query($query) or die(mysql_error());
            return mysql_fetch_array($sql);
        }
        protected function get_list_data_db($query) {
        	$sql = mysql_query($query) or die(mysql_error());
            $array = array();
			while ($row = mysql_fetch_array($sql)) {
				$array[] = $row;
			}
			return $array;
        }
	    function get_max_thutu_db($table) {
			$query = "SELECT thutu FROM ".$table." ORDER BY thutu DESC limit 0,1";
			$sql = mysql_query($query) or die(mysql_error());
			$details = mysql_fetch_array($sql);
			return $details['thutu'];
		}
	    protected function get_by_thutu_db($table,$id=-1,$anhien=-1,$dk1='',$dk2='') {
			settype($id, "int");
			settype($anhien, "int");
			$query = "SELECT * FROM ".$table." WHERE (anhien='$anhien' or '$anhien'=-1) and (id='$id' or '$id'=-1) ".$dk1." order by thutu desc ".$dk2;
                        //echo $query;
                        if ($id==-1)
				return $this->get_list_data_db($query);
			else 
				return $this->get_data_db($query);
		}
		protected function get_by_ngay_db($table,$id=-1,$anhien=-1,$dk1='',$dk2='') {
			settype($id, "int");
			settype($anhien, "int");
			$query = "SELECT * FROM ".$table." WHERE (anhien='$anhien' or '$anhien'=-1) and (id='$id' or '$id'=-1) ".$dk1." order by ngay DESC ".$dk2;
			if ($id==-1) 
				return $this->get_list_data_db($query);
			else 
				return $this->get_data_db($query);
		}
    	function get_by_parent_db($table,$table_parent,$field,$idparent) {
			$query = "SELECT ".$table.".* FROM ".$table.
			",".$table_parent." WHERE ".$table.".".$field."=".$table_parent.".id 
			and ".$table.".".$field."=".$idparent." order by thutu desc";
			return $this->get_list_data_db($query);
		}
    	protected function get_max_count_db($table,$text="",$anhien=-1) {
    		$sql = "and (ten_vi like '%".$text."%' or ten_en like '%".$text."%')";
    		if ($table=="user")
    			$sql = "and (username like '%".$text."%' or hoten like '%".$text."%')";
			$query = "SELECT * FROM ".$table." WHERE 1 ".$sql." and (anhien='$anhien' or '$anhien'=-1) order by thutu desc";
            return count($this->get_list_data_db($query));
		}
    	protected function get_by_thutu_limit_db($table,$offset,$records_per_page,$text="",$anhien=-1) {
			settype($offset, "int");
			settype($records_per_page, "int");
			settype($anhien, "int");
			$sql = "and (ten_vi like '%".$text."%' or ten_en like '%".$text."%')";
    		if ($table=="user")
    			$sql = "and (username like '%".$text."%' or hoten like '%".$text."%')";
			$query = "SELECT * FROM ".$table." WHERE 1 ".$sql." and (anhien='$anhien' or '$anhien'=-1) order by thutu desc LIMIT ".$offset.",".$records_per_page;
			return $this->get_list_data_db($query);
		}
    	protected function get_by_ngay_limit_db($table,$offset,$records_per_page,$text="",$anhien=-1) {
			settype($offset, "int");
			settype($records_per_page, "int");
			settype($anhien, "int");
			$sql = "and (ten_vi like '%".$text."%' or ten_en like '%".$text."%')";
    		if ($table=="user")
    			$sql = "and (username like '%".$text."%' or hoten like '%".$text."%')";
			$query = "SELECT * FROM ".$table." WHERE 1 ".$sql." and (anhien='$anhien' or '$anhien'=-1) order by ngay DESC LIMIT ".$offset.",".$records_per_page;
			return $this->get_list_data_db($query);
		}
		
	    protected function update_thutu_db($table, $id, $thutu) {
			$query = "UPDATE ".$table." SET thutu='$thutu' WHERE id='$id'";
			mysql_query($query) or die(mysql_error());
		}
		
		protected function update_all_thutu_db($table, $thutu_cu, $thutu_moi) {
			$query = "UPDATE ".$table." SET thutu='$thutu_moi' WHERE thutu='$thutu_cu'";
			mysql_query($query) or die(mysql_error());
		}
		
	    function move_record_db($table, $id, $loai_move, $anhien=-1) {
			$details = $this->get_by_thutu_db($table,$id,$anhien);
			if (($loai_move=="up" || $loai_move=="down") && $details!="") {
				if ($loai_move=="up") 
					$thutu = $details['thutu']-1;
				if ($loai_move=="down") 
					$thutu = $details['thutu']+1;
				$this->update_all_thutu_db($table, $thutu, $details['thutu']);
				$this->update_thutu_db($table, $id, $thutu);
			}
		}
		
		function check_isset($check_isset) {
			foreach ($check_isset as $fieldname) { 
				if(!isset($_POST[$fieldname])) { 
					return false;
				} 
			} 
			return true;
		}
		
		function check_empty($check_empty) {
			foreach ($check_empty as $fieldname) { 
				if(empty ($_POST[$fieldname])) { 
					return false;
				} 
			}
			return true; 
		}
		
    	function update_field_db($table,$id,$field,$value,$anhien=-1) {
	   	 	$details = $this->get_by_thutu_db($table,$id,$anhien);
			if ($details!="") {
				$query = "UPDATE ".$table." SET ".$field."='$value' WHERE id='$id'";
				mysql_query($query) or die(mysql_error());
			}
        }
        
    	protected function change_anhien_db($table,$id) {
	   	 	$details = $this->get_by_thutu_db($table,$id,-1);
			if ($details!="") {
				if ($details['anhien']==0)
					$this->update_field_db($table,$id,"anhien",1);
				elseif ($details['anhien']==1)
					$this->update_field_db($table,$id,"anhien",0);
			}
        }
    	protected function change_anhien_list_db($table,$listid) {
        	$idlist = explode(",",$listid); 
			for ($i=0 ; $i<count($idlist) ; $i++){
				$id=$idlist[$i]; 
				$this->change_anhien_db($table,$id);
			}	
        }
		
   	 	function delete_data_db($table,$id) {
	   	 	$details = $this->get_by_thutu_db($table,$id,-1);
			if ($details!="") {
				$query = "DELETE FROM ".$table." WHERE id='$id'";
				mysql_query($query) or die(mysql_error());
			}
        }
		function delete_object_db($table,$where) {
				$query = "DELETE FROM ".$table." ".$where;
				//echo $query;die;
				mysql_query($query) or die(mysql_error());
        }
    	function delete_list_data_db($table,$listid) {
        	$idlist = explode(",",$listid); 
			for ($i=0 ; $i<count($idlist) ; $i++){
				$id=$idlist[$i]; 
				$this->delete_data_db($table,$id);
			}	
        }
        
	    function check_valid_email($email) {
			$pattern = '/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/';
			if(preg_match($pattern, $email)==0) 
				return false;
			else 
				return true;
		}
		function get_short_page($table,$page,$page_count,$text="") {
    		settype($page, "int");
			settype($page_count, "int");
			$query = "";
			if ($text!="")
				$query = $query."&text=".$text;
			if ($page_count>1) {
				if ($page-1 > 0)
					$prev = '<< <a href="?p='.$table.'_quanly&page='.($page-1).$query.'">Prev</a>';
				else
					$prev = '<< Prev';
					
				if($page < $page_count)
					$next = '<a href="?p='.$table.'_quanly&page='.($page+1).$query.'">Next</a> >>';
				else
					$next = 'Next >>';
					
				$page_select = '<select id="change_page_select" onchange="root_page(this.value)">';
				for($i=0;$i<$page_count ; $i++){
					if($page==$i+1)
						$page_select = $page_select.'<option value="?p='.$table.'_quanly&page='.($i+1).$query.'" selected>'.($i+1).'</option>';
					else
						$page_select = $page_select.'<option value="?p='.$table.'_quanly&page='.($i+1).$query.'">'.($i+1).'</option>';
				}
				$page_select = $page_select.'</select>';
					
				echo $prev.' | Page '.$page.'/'.$page_count.' | '.$next.'&nbsp;&nbsp;'.$page_select;
			}
		}
		// Đừng đụng những phần trên
		 function get_by_thutu($table,$id=-1,$anhien=-1,$dk1='',$dk2='') {
			settype($id, "int");
			settype($anhien, "int");
			$query = "SELECT * FROM ".$table." WHERE (anhien='$anhien' or '$anhien'=-1) and (id='$id' or '$id'=-1) ".$dk1." order by thutu desc ".$dk2;
                     //   echo $query;
			$kq=mysql_query($query) or die(mysql_error());
			return $kq;
		}
		protected function get_by_ngay($table,$id=-1,$anhien=-1,$dk1='',$dk2='') {
			settype($id, "int");
			settype($anhien, "int");
			$query = "SELECT * FROM ".$table." WHERE (anhien='$anhien' or '$anhien'=-1) and (id='$id' or '$id'=-1) ".$dk1." order by ngay DESC ".$dk2;
			$kq=mysql_query($query) or die(mysql_error());
			return $kq;
		}
		function get_by_id($table,$id=-1,$anhien=-1,$dk1='',$dk2='') {
			settype($id, "int");
			settype($anhien, "int");
			$query = "SELECT * FROM ".$table." WHERE (anhien='$anhien' or '$anhien'=-1) and (id='$id' or '$id'=-1) ".$dk1." order by id desc ".$dk2;
                     //   echo $query;
			$kq=mysql_query($query) or die(mysql_error());
			return $kq;
		}
		function get_thutu($table) {
			$sql="select thutu from $table order by thutu desc limit 0,1";
			$kq=mysql_query($sql) or die(mysql_error()); 
			$row=mysql_fetch_assoc($kq);
			return ($row['thutu']+1);		
		}
		function anhien($table,$id,$loai)
		{
			$query="select ".$loai." from ".$table." where id=$id";
			$rs = mysql_query($query) or die(mysql_error());
			$row_rs = mysql_fetch_row($rs);
			$anhien = $row_rs[0];
			if($anhien==1) $anhien=0; else $anhien=1;
			$query = "UPDATE ".$table." SET ".$loai." = $anhien where id=$id";
			mysql_query($query) or die(mysql_error()); 
			return $anhien;		
		}
		function insertObject($table,$field,$value){
			$str = "insert into $table ($field) value($value)";
			//echo $str;die;
			$res = mysql_query($str) or die(mysql_error());
			if($res){
				return $res;
			}else return "<h1 style='color:#FF0000'>"."InsertObject Error!"."</h1>";
		}
		function updateObject($table,$field=array(),$value=array(),$where){
		$count = count($field);
		$check=0; // 0 as true
		for($i=0;$i<$count;$i++){
			$str = "update $table set $field[$i]=$value[$i] where $where";
			//echo $str;
			$res=mysql_query($str);
			if($res)$check=0; else $check=1;
		}
		if($check==1){
			return "<h1 style='color:#FF0000'>"."updateObject Error!"."</h1>";
		}else return $res;
		
		}
		
		
		function get_phantrang_db($pageNum=1,$pageSize=9,&$totalRows,$table,$id=-1,$anhien=-1,$dk1='',$dk2='') {
			settype($id, "int");
			settype($anhien, "int");
			settype($pageNum, "int");
			if($pageNum<=0) $pageNum=1;
			 $totalRows = 0;
			$query = "SELECT count(id) FROM ".$table." WHERE (anhien='$anhien' or '$anhien'=-1) and (id='$id' or '$id'=-1) ".$dk1." order by thutu desc ".$dk2;
			//echo $query;die;
			$kq = mysql_query($query) or die (mysql_error());
            $row_kq = mysql_fetch_row($kq);
            $totalRows = $row_kq[0];

            $startRow = ($pageNum - 1)*$pageSize;
			
			$query = "SELECT * FROM ".$table." WHERE (anhien='$anhien' or '$anhien'=-1) and (id='$id' or '$id'=-1) ".$dk1." order by thutu desc ".$dk2." LIMIT $startRow , $pageSize";
			//echo $query;die;
           	$kq = mysql_query($query) or die(mysql_error());
			return $kq;
		}
		protected function get_phantrang_order($pageNum=1,$pageSize=9,&$totalRows,$table,$id=-1,$anhien=-1,$dk1='',$dk2='') {
			settype($id, "int");
			settype($anhien, "int");
			settype($pageNum, "int");
			if($pageNum<=0) $pageNum=1;
			 $totalRows = 0;
			$query = "SELECT count(id) FROM ".$table." WHERE (anhien='$anhien' or '$anhien'=-1) and (id='$id' or '$id'=-1) ".$dk1.$dk2;
			//echo $query;die;
			$kq = mysql_query($query) or die (mysql_error());
            $row_kq = mysql_fetch_row($kq);
            $totalRows = $row_kq[0];

            $startRow = ($pageNum - 1)*$pageSize;
			
			$query = "SELECT * FROM ".$table." WHERE (anhien='$anhien' or '$anhien'=-1) and (id='$id' or '$id'=-1) ".$dk1.$dk2." LIMIT $startRow , $pageSize";
			//echo $query;die;
           	$kq = mysql_query($query) or die(mysql_error());
			return $kq;
		}
		protected function get_phantrang_db_ngay($pageNum=1,$pageSize=9,&$totalRows,$table,$id=-1,$anhien=-1,$dk1='',$dk2='') {
			settype($id, "int");
			settype($anhien, "int");
			settype($pageNum, "int");
			if($pageNum<=0) $pageNum=1;
			 $totalRows = 0;
			$query = "SELECT count(id) FROM ".$table." WHERE (anhien='$anhien' or '$anhien'=-1) and (id='$id' or '$id'=-1) ".$dk1." order by ngay desc ".$dk2;
			//echo $query;die;
			$kq = mysql_query($query) or die (mysql_error());
            $row_kq = mysql_fetch_row($kq);
            $totalRows = $row_kq[0];

            $startRow = ($pageNum - 1)*$pageSize;
			
			$query = "SELECT * FROM ".$table." WHERE (anhien='$anhien' or '$anhien'=-1) and (id='$id' or '$id'=-1) ".$dk1." order by ngay desc ".$dk2." LIMIT $startRow , $pageSize";
			//echo $query;die;
           	$kq = mysql_query($query) or die(mysql_error());
			return $kq;
		}
           function pagesList($totalRows , $pageNum=1, $pageSize = 5, $offset = 1){
			//echo $totalRows;die;
			$baseURL = $_SERVER['PHP_SELF'];
            parse_str($_SERVER['QUERY_STRING'],$arr);
            unset($arr['pageNum']);
            foreach($arr as $k=> $v) $str.= "&{$k}={$v}";
            $baseURL .="?".$str;

            if ($totalRows<=0) return "";
            $totalPages = ceil($totalRows/$pageSize);
            if ($totalPages<=1) return "";

            $prevLink="";$nextLink="";

            if ($pageNum > 1) {
			$prevPage = $pageNum - 1;
			$prevLink="<span class='showpage'><a href='$baseURL&pageNum=$prevPage'>Trang trước</a></span> &nbsp; |&nbsp;";
            }
            if ($pageNum < $totalPages) {
			$nextPage = $pageNum + 1;
			$nextLink = " &nbsp; |&nbsp; <span class='showpage'><a href='$baseURL&pageNum=$nextPage'>Trang kế</a></span> ";
            }

            $from = $pageNum - $offset;
            $to = $pageNum + $offset;
            if ($from <=0) { $from = 1;   $to = $offset*2; }
            if ($to > $totalPages) { $to = $totalPages; }
            $links = "";
            for($j = $from; $j <= $to; $j++) {
                    if ($j==$pageNum) $links= $links . "<span class='showpagePoint'>$j</span> ";
                    else $links= $links . " <span class='showpageNum'><a href = '$baseURL&pageNum=$j'>$j</a></span> ";
            } //for
			$page_select = '&nbsp;&nbsp;<select id="change_page_select" onchange="root_page(this.value)">';
			for($i=0;$i<$totalPages ; $i++){
					if($pageNum==$i+1)
						$page_select = $page_select.'<option value="'.$baseURL.'&pageNum='.($i+1).$query.'" selected>'.($i+1).'</option>';
					else
						$page_select = $page_select.'<option value="'.$baseURL.'&pageNum='.($i+1).$query.'">'.($i+1).'</option>';
			}
			$page_select = $page_select.'</select>';
			
            return $prevLink.$links.$nextLink.$page_select;
            } // function pagesList

            function pagesList1($baseURL,$totalRows , $pageNum=1, $pageSize = 5, $offset = 5){

            if ($totalRows<=0) return "";
            $totalPages = ceil($totalRows/$pageSize);
            if ($totalPages<=1) return "";

            $prevLink="";$nextLink="";

            if ($pageNum > 1) {
			$prevPage = $pageNum - 1;
			$prevLink="<span class='showpage'><a href='$baseURL/$prevPage'>Trang trước</a></span>";
            }
            if ($pageNum < $totalPages) {
			$nextPage = $pageNum + 1;
			$nextLink = "<span class='showpage'><a href='$baseURL/$nextPage'>Trang kế</a></span>";
            }

            $from = $pageNum - $offset;
            $to = $pageNum + $offset;
            if ($from <=0) { $from = 1;   $to = $offset*2; }
            if ($to > $totalPages) { $to = $totalPages; }
            $links = "";
            for($j = $from; $j <= $to; $j++) {
                    if ($j==$pageNum) $links= $links . "<span class='showpagePoint'>$j</span>";
                    else $links= $links . "<span class='showpageNum'><a href = '$baseURL/$j'>$j</a></span>";
            } //for
            return $prevLink.$links.$nextLink;
            } // function pagesList
			
			function pagesList2($totalRows , $pageNum=1, $pageSize = 5, $offset = 5){
			//echo $totalRows;die;
			$baseURL = $_SERVER['PHP_SELF'];
            parse_str($_SERVER['QUERY_STRING'],$arr);
            unset($arr['pageNum']);
            foreach($arr as $k=> $v) $str.= "&{$k}={$v}";
            $baseURL .="?".$str;

            if ($totalRows<=0) return "";
            $totalPages = ceil($totalRows/$pageSize);
            if ($totalPages<=1) return "";

            $prevLink="";$nextLink="";

            if ($pageNum > 1) {
			$prevPage = $pageNum - 1;
			$prevLink="<span class='showpage'><a href='$baseURL&pageNum=$prevPage'>Trang trước</a></span>";
            }
            if ($pageNum < $totalPages) {
			$nextPage = $pageNum + 1;
			$nextLink = "<span class='showpage'><a href='$baseURL&pageNum=$nextPage'>Trang kế</a></span>";
            }

            $from = $pageNum - $offset;
            $to = $pageNum + $offset;
            if ($from <=0) { $from = 1;   $to = $offset*2; }
            if ($to > $totalPages) { $to = $totalPages; }
            $links = "";
            for($j = $from; $j <= $to; $j++) {
                    if ($j==$pageNum) $links= $links . "<span class='showpagePoint'>$j</span>";
                    else $links= $links . "<span class='showpageNum'><a href = '$baseURL&pageNum=$j'>$j</a></span>";
            } //for
            return $prevLink.$links.$nextLink;
            } // function pagesList
            function stripUnicode($str){
	if(!$str) return false;
	$unicode = array(
	 'a'=>'á|à|ả|ã|ạ|ă|ắ|ằ|ẳ|ẵ|ặ|â|ấ|ầ|ẩ|ẫ|ậ',
	 'A'=>'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ằ|Ẳ|Ẵ|Ặ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
	 'd'=>'đ',
	 'D'=>'Đ',
	 'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',
	 'E'=>'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
	 'i'=>'í|ì|ỉ|ĩ|ị',
	 'I'=>'Í|Ì|Ỉ|Ĩ|Ị',
	 'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',
	 'O'=>'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
	 'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',
	 'U'=>'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
	 'y'=>'ý|ỳ|ỷ|ỹ|ỵ',
	 'Y'=>'Ý|Ỳ|Ỷ|Ỹ|Ỵ'
	);
	foreach($unicode as $khongdau=>$codau) {
	  $arr = explode("|",$codau);
	  $str = str_replace($arr,$khongdau,$str);
	}
	return $str;
        }
        function changeTitle($str){
	$str = $this->stripUnicode($str);
	$str = str_replace("?","",$str);
	$str = str_replace("&","",$str);
	$str = str_replace("'","",$str);
	$str = str_replace("  "," ",$str);
	$str = trim($str);
	$str = mb_convert_case($str , MB_CASE_TITLE , 'utf-8');
// MB_CASE_UPPER/MB_CASE_TITLE/MB_CASE_LOWER
	$str = str_replace(" ","-",$str);
	return $str;
    }
}
?>